// pages/user/business-upgrade/business-upgrade.js
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog';
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    submitDis: false,
    isShowEdition: false,
    editionColumns: [],
    edition: '',

    dataOrderId: '',
  },

  //预提交接口
  prepare() {
    let _this = this;
    if(_this.data.edition == ''){
      Toast('请选择新版本'); return;
    }
    _this.setData({submitDis: true})
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/order/precreate',
      data: {
        memberId: _this.data.user.id,
        isCentreord: 1,
        projectExpId: _this.data.edition.id,
        products: [{
          cartId: 0,
          productId: 0,
          total: 1,
          marketprice: _this.data.edition.price,
        }]
      },
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': wx.getStorageSync('authToken') || 'empty-token'
      },
    }).then(res =>{
      //添加订单
      _this.newCreat();
    })
  },

  //添加订单接口
  newCreat() {
    let _this = this;
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/order/create',
      data: {
        isupgrade: 1,//版本升级关键标识
        memberId: _this.data.user.id,
        referrals: '',//推荐人id
        products: [{
          id: 0 + ':null',
          productId: 0,
          total: 1,
          mid: _this.data.user.id,
          openid: _this.data.user.openid,
          teamid: _this.data.user.upshopTid,
          centreId: _this.data.user.centreId,
        }],
        sname: _this.data.user.nickname,
        mobile: _this.data.user.mobile,
        houseNo: "",
        couponId: null,
        Authorization: wx.getStorageSync('authToken'),
        allweight: 0,
        dispatchprice: 0,
        dispatchid: 0,
        isCentreord: 1,//传1为版本订单
        projectExpId: _this.data.edition.id
      },
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': wx.getStorageSync('authToken') || 'empty-token'
      },
    }).then(res =>{
      //获取订单id
      _this.data.dataOrderId = res.data.data;

      //准备支付
      _this.orderOrder();
    })
  },

  //准备支付
  orderOrder() {
    let _this = this;
    let shop = app.getShop();
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/order/',
      data: {
        orderId: _this.data.dataOrderId,
        id: '',
        iskuaidi: '',
        shopId: shop ? shop.id : '',
        Authorization: wx.getStorageSync('authToken'),
        number: Math.random()
      }
    }).then(res =>{
      //支付
      _this.paymentMoney();
    })
  },
  //支付
  paymentMoney(){
    let _this = this;
    if(!_this.data.user.appOpenid){//支付授权
      wx.navigateTo({
        url: '/pages/login-pay/login-pay'
      })
    } else {
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/order/apppay',
        data: {
          orderId: _this.data.dataOrderId,
          couponId: '',
          points: '',
        }
      }).then(res =>{
        let party = {};
        let respData = res.data.data.respData;
        if(respData){
          party.addId = respData.payAppId;
          party.nonceStr = respData.paynonceStr;
          party.package = respData.payPackage;
          party.paySign = respData.paySign;
          party.signType = respData.paySignType;
          party.timeStamp = respData.payTimeStamp;
        }
        //兼容微信支付，第三方支付
        let last = respData ? party : res.data.data;
        wx.requestPayment({
          ...last,
          success: function(d) {
            Dialog.alert({
              title: '提示',
              message: '支付成功，请等待审核'
            }).then(res =>{
              wx.switchTab({
                url: '/pages/user/user',
              })
            })
          },
          fail: function(d) {
            Dialog.alert({
              title: '提示',
              message: '支付失败'
            })
          }
        })
      }).finally(res =>{
        console.log(res)
      })
    }
  },

  //版本数据源
  columnsEditionData() {
    let _this = this;
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/getProjectExp',
      data: {
        projectPrice: _this.data.user.projectexpPrice,
        projectExpId: _this.data.user.projectexpId,
        offset: 0,
        limit: 0,
      },
    }).then(res =>{
      _this.setData({
        editionColumns: res.data.data.rows
      })
    })
  },

  editionConfirm(event){
    this.setData({
      edition: event.detail.value
    })
    this.editionMonitor();
  },
  editionMonitor(){
    let _this = this;
    _this.setData({
      isShowEdition: !_this.data.isShowEdition
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user: app.getUser(),
    })
    this.columnsEditionData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})